﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Data.SqlClient;
using System.Web.SessionState;

public partial class SysManage_Menu : System.Web.UI.Page
{
    public SqlConnection myconn;
    public SqlCommand mycomm;
    public SqlDataReader mydr;
    public SqlDataAdapter myadapter;
    public DataSet myds;
    public DataView mydv;
    public string sHtml = "";
    protected BaiMaSoft.Conn.DataConn Myconn = new BaiMaSoft.Conn.DataConn();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ShowMenuTree();
        }
    }

    public void ShowMenuTree()
    {
        /* 控件树 */
        string sRootLeaflessIconHtml = "<img height=\"20\" src=\"img/sys/root_leafless.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sMidLeaflessIconHtml = "<img height=\"20\" src=\"img/sys/mid_leafless.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sRearLeaflessIconHtml = "<img height=\"20\" src=\"img/sys/rear_leafless.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sPageUnselectedIconHtml = "<img height=\"20\" src=\"img/sys/page_unselected.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sVerticalIconHtml = "<img height=\"20\" src=\"img/sys/vertical.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //string sButtonIconHtml = "<img height=\"20\" src=\"img/sys/button.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        string sBlankIconHtml = "<img src=\"img/sys/blank.gif\" align=\"absmiddle\" border=\"0\" WIDTH=\"19\" HEIGHT=\"20\">";

        try
        {
            Myconn.Connection.Open();

            //UserInfoBean userInfo = (UserInfoBean)pageContext.getSession().getAttribute("userInfo");
            string sUserRoleCode = Session["Baimasoft_Role_Code"].ToString().Trim();
            string sUserID = Session["Baimasoft_User_LogonID"].ToString().Trim();
            string proc = "";


            // 判断当前用户的功能角色是否有效
            // 0 根目录 1 系统权限功能 2 为A系统 3为B系统 4为C系统 5......
            //proc = "select * from Sys_Menu_MyTree where Role_Code='" + sUserRoleCode + "' Order by Menu_Code";
            proc = @"
                    SELECT a.*
                    FROM Sys_Menu_MyTree a LEFT OUTER JOIN
                          Sys_Menu_Tree b ON a.Menu_Code = b.Code " +
                   @" WHERE (a.Role_Code = '" + sUserRoleCode + "' ) AND (b.Category IN (0, 1,2)) " +
                    " ORDER BY a.Menu_Code  ";
            //}
            mycomm = new SqlCommand(proc, Myconn.Connection);
            SqlDataReader ShowTree = mycomm.ExecuteReader();

            while (ShowTree.Read())
            {
                //从数据库存储过程所创建的临时表中取数据
                string sIndentVertical = ShowTree["IndentVertical"].ToString().Trim(); //展开结点树时，缩进的结点前面是否有上(几)级结点的垂线
                string isLastSibling = ShowTree["LastSibling"].ToString(); //是否同族的最后一个结点

                string isLeafed = ShowTree["Leafed"].ToString(); //是否有叶子结点

                string sCode = ShowTree["Menu_Code"].ToString(); // 结点编码
                string cType = ShowTree["Type"].ToString(); //结点类型
                int iHomologID = (int)ShowTree["Page_ID"]; //若是页面其对应的页面ID
                string sLabel = ShowTree["Label"].ToString(); //结点标签
                string sLink = ShowTree["Link"].ToString(); //若是页面结点其对应的链接
                string sIndicator = ShowTree["Indicator"].ToString(); //导航条

                string sFolderCloseIconHtml = "<img id=\"folder_" + sCode + "\"height=\"20\" src=\"img/sys/folder_close.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
                string sMidRetractIconHtml = "<img id=\"flex_" + sCode + "\" style=\"cursor:pointer\" onClick=\"flex(this)\" height=\"20\" src=\"img/sys/mid_retract.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
                string sRearRetractIconHtml = "<img id=\"flex_" + sCode + "\" style=\"cursor:pointer\" onClick=\"flex(this)\" height=\"20\" src=\"img/sys/rear_retract.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";

                /* 组合html语句 */
                string sLabelHtml;
                if (cType.Equals("0"))
                {
                    string[] asLink = sLink.Split('?');
                    if (asLink.Length > 1)
                    {
                        sLabelHtml = "<a href=\"" + sLink + "&PageID=" + iHomologID + "&sIndicator=" + sIndicator + "\" onclick=\" tog(this,'#E6EDF7')\"  target=\"main\"><font color=\"#000000\" >" + sLabel + "</font></a>";
                    }
                    else
                    {
                        sLabelHtml = "<a href=\"" + sLink + "?PageID=" + iHomologID + "&sIndicator=" + sIndicator + "\" onclick=\" tog(this,'#E6EDF7')\"  target=\"main\"><font color=\"#000000\" >" + sLabel + "</font></a>";
                    }
                }
                else
                    sLabelHtml = "<label id=\"label_folder_" + sCode + "\" onClick=\"flexTreeByLabel(this)\"><font color=\"#000000\" >" + sLabel + "</font></label>";

                if (sCode.Equals("Men"))
                { //根结点

                    string sRootHtml = "";
                    if (isLeafed.Equals("0"))
                    { //无子结点
                        sRootHtml = sRootLeaflessIconHtml + sFolderCloseIconHtml + sLabelHtml;
                        break;
                    }
                    else
                    {
                        string sRootRetractIconHtml = "<img id=\"flex_Men\" style=\"cursor:pointer\" onClick=\"flex(this)\" height=\"20\" src=\"img/sys/root_retract.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
                        sRootHtml = sRootRetractIconHtml + sFolderCloseIconHtml + sLabelHtml;
                    }
                    sHtml = sHtml + "<div id=\"Men\">" + sRootHtml + "</div>";
                }
                else
                { //非根结点
                    string sNodeHtml = "";
                    /* 处理缩进 */
                    string sIndentHtml = "";
                    int i = 0;
                    char cIsVertical;
                    while (i < sIndentVertical.Length)
                    {
                        cIsVertical = sIndentVertical[i];
                        if (cIsVertical == '0')
                            sIndentHtml = sIndentHtml + sBlankIconHtml;
                        else
                            sIndentHtml = sIndentHtml + sVerticalIconHtml;
                        i = i + 1;
                    }

                    /* 结点 */
                    if (cType.Equals("1"))
                    { //1.树枝结点
                        if (isLastSibling.Equals("0"))
                        { //2.非尾结点
                            if (isLeafed.Equals("1")) //3.有子结点
                                sNodeHtml = sMidRetractIconHtml + sFolderCloseIconHtml + sLabelHtml;
                            else //3.无子结点
                                sNodeHtml = sMidLeaflessIconHtml + sFolderCloseIconHtml + sLabelHtml;
                        }
                        else
                        { //2.尾结点

                            if (isLeafed.Equals("1")) //3.有子结点
                                sNodeHtml = sRearRetractIconHtml + sFolderCloseIconHtml + sLabelHtml;
                            else //3.无子结点
                                sNodeHtml = sRearLeaflessIconHtml + sFolderCloseIconHtml + sLabelHtml;
                        }
                    }
                    else if (cType.Equals("0"))
                    { //1.树叶结点
                        if (isLastSibling.Equals("0"))  //2.非尾结点
                            sNodeHtml = sMidLeaflessIconHtml + sPageUnselectedIconHtml + sLabelHtml;
                        else  //2.尾结点

                            sNodeHtml = sRearLeaflessIconHtml + sPageUnselectedIconHtml + sLabelHtml;
                    }

                    sNodeHtml = sIndentHtml + sNodeHtml;
                    sHtml = sHtml + "<div id=\"" + sCode + "\" style=\"display:none\">" + sNodeHtml + "</div>";
                }
            }

            sHtml = sHtml + "<script>spreadTree(\"Men001\");</script>";

            if (ShowTree != null)
                ShowTree.Close();

            if (Myconn.Connection != null)
                Myconn.Connection.Close();
        }
        catch
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请刷新页面!');</script>");
        }
    }
}
